import * as PIXI from 'pixi.js';
import BaseLogic from './BaseLogic';
import ArrayUtil from '../util/ArrayUtil';
import ReferViews from '../core/ReferViews';

// import Hammer from 'hammerjs';

export default class NearLogic extends BaseLogic{

    xList:number[] = [];
    yList:number[] = [];
    offset:number = 50;
    
    initX(min:number, max:number, excludes:number[]){
        var xList:number[] = [];
        this.points.forEach(v => {
            if(v.x > min && v.x < max){
                xList.push(v.x);
            }
        })
        ArrayUtil.remove(xList, excludes);
        this.xList = ArrayUtil.uniq(xList);
        console.log(this.xList, "near x");
    }

    initY(min:number, max:number, excludes:number[]){
        var yList:number[] = [];
        this.points.forEach(v => {
            if(v.y > min && v.y < max){
                yList.push(v.y);
            }
        })
        ArrayUtil.remove(yList, excludes);
        this.yList = ArrayUtil.uniq(yList);
        console.log(this.yList, "near y");
    }

    nearX(x:number){
        var min = 0;
        var distance = this.offset;
        this.xList.forEach(n => {
            var w = Math.abs(n - x);
            if(w < distance){
                min = n;
                distance = w;
            }
        })
        return min;
    }

    nearY(y:number){
        var min = 0;
        var distance = this.offset;
        this.yList.forEach(n => {
            var w = Math.abs(n - y);
            if(w < distance){
                min = n;
                distance = w;
            }
        })
        return min;
    }

    get points():PIXI.Point[]{
        return ReferViews.instance.draw.shapeView.points;
    }
}